package ltd.dujiabao.orderservice.dao;

import ltd.dujiabao.orderservice.domain.entity.Order;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;

/**
 * @author Bowie Du
 * @date 2025/7/9 21:56
 */

@Repository
public interface OrderRepository extends CrudRepository<Order, Long> {
    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query("update Order set status = :status where id = :id")
    int updateStatusById(Long id, String status);

    @Transactional(rollbackFor = Exception.class)
    @Modifying(clearAutomatically = true, flushAutomatically = true)
    @Query("update Order set status = :newStatus where id = :id and status in (:oldStatusList)")
    int compareAndSetStatusById(Long id, Collection<String> oldStatusList, String newStatus);
}
